// pages/list/list.js
const apiurl = require('../../config.js').apiurl;
import { Pulibc } from '../../models/public.js'
const pulibc = new Pulibc();
Page({
  chengDetails(e){
    // console.log(e.currentTarget.dataset.goods_id)
    var id = e.currentTarget.dataset.goods_id
    pulibc.navigateUrl('../details-goods/details-goods?id=' + id)
  },
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    type: 1,
    priceSwitch: 0,
    sellSwitch: 0,
    priceType: 3,
    sellType: 5,
    member_gradeid:0 //会员等级
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMemberInfo();
    var type = 1;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: apiurl + '/goods/goods_list',
      data: { type: type },
      success: data => {
        // var datalist=data;
        console.log("shopdata", data);
        this.setData({
          list: data.data.datas.goods,
        })
        wx.hideLoading();
      }
    });
  },
  // 获取会员接口
  getMemberInfo() {
    var keyVal = wx.getStorageSync('key');
    console.log(keyVal)
    wx.request({
      url: apiurl + '/member/index',
      data: {
        key: keyVal
      },
      success: data => {
        console.log(data.data.datas.member_info)
        if (data.data.code == 200) {
          var member_gradeid = data.data.datas.member_info.member_gradeid;
          // member_gradeid 0 非会员 1会员，2代理，3经销商，4众筹，5众筹+股份
          this.setData({
            member_gradeid
          })
        }
      }
    })
  },
  // 综合分类
  bindTypeTap(event) {
    var type = event.currentTarget.dataset.type;
    console.log("type=>", type)
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: apiurl + '/goods/goods_list',
      data: { type: type },
      success: data => {
        // var datalist=data;
        console.log("shopdata", data);
        this.setData({
          list: data.data.datas.goods,
          type: type,
          priceSwitch: 0,
          sellSwitch: 0,
          priceType: 3,
          sellType: 5,
        })
        wx.hideLoading();
      }
    });
  },
  // 价格排序
  bindPriceTap(event) {
    console.log("priceType=>", this.data.priceType);
    wx.showLoading({
      title: '加载中',
    })
    var type = event.currentTarget.dataset.type;
    var priceType, priceSwitch;
    if (this.data.priceType == 3) {
      //  升序
      priceType = 2;
      priceSwitch = 1;
    } else {
      //  降序
      priceType = 3;
      priceSwitch = 2;
    }
    wx.request({
      url: apiurl + '/goods/goods_list',
      data: { type: priceType },
      success: data => {
        this.setData({
          list: data.data.datas.goods,
          type: type,
          priceSwitch: priceSwitch,
          priceType: priceType,
          sellSwitch:0,   //恢复销售初始状态
          sellType: 5
        })
        // 隐藏loading
        wx.hideLoading();
      }
    })
  },
  // 销售排序
  bindSellTap(event) {
    console.log("sellType=>", this.data.sellType);
    wx.showLoading({
      title: '加载中',
    });
    var type = event.currentTarget.dataset.type;
    var sellType, sellSwitch;
    if (this.data.sellType == 5) {
      // 升序
      sellType = 4;
      sellSwitch = 1;
    } else {
      // 降序
      sellType = 5;
      sellSwitch = 2;
    }
    wx.request({
      url: apiurl + '/goods/goods_list',
      data: { type: sellType },
      success: data => {
        this.setData({
          list: data.data.datas.goods,
          type: type,
          sellSwitch: sellSwitch,
          sellType: sellType,
          priceSwitch:0,  //恢复价格初始状态
          priceType:3
        })
        // 隐藏loading
        wx.hideLoading();
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.getStorageSync("key") == false) {
      wx.navigateTo({
        url: "../register/register"
      })
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})